<template>
  <el-table ref="multipleTableRef" :data="dataSource.data" style="width: 100%" :row-key="rowKey"
    :default-expand-all="defaultExpandAll" @sort-change="handleSortChange" @filter-change="handleFilterChange">
    <el-table-column v-for="column in computedColumns" :key="column.prop" :prop="column.prop" :label="column.label"
      :width="column.width" :type="column.type" :filters="column.filters" :sortable="column.sortable"
      :sort-orders="column.sortOrders" :column-key="column.columnKey" :filter-multiple="column.filterMultiple">
      <template v-if="column.slot" #default="scope">
        <slot :name="column.slot" :row="scope.row" />
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination">
    <el-pagination v-if="dataSource?.pigination" :current-page="dataSource?.pigination?.current"
      :page-size="dataSource?.pigination?.size" :page-sizes="[10, 20, 30, 40, 50]"
      layout="total, sizes, prev, pager, next, jumper" :total="dataSource?.pigination?.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>

<script lang="ts" setup>
import { ElTable } from "element-plus";
import { computed, onMounted, reactive, ref } from "vue";
import { IFilterChangeParams, ISortChangeParams, ITableProps } from "./type";
interface IProps {
  dataSource: ITableProps<any>;
  defaultExpandAll?: boolean;
  rowKey?: string;
  tableRef?: InstanceType<typeof ElTable>;
}
const { dataSource, defaultExpandAll, rowKey } = defineProps<IProps>();

const emits = defineEmits<{
  (event: "sizeChange", pageSize: number): void;
  (event: "currentChange", currentPage: number): void;
  (event: "sortChange", data: ISortChangeParams): void;
  (event: "filterChange", data: IFilterChangeParams): void;
}>();

const multipleTableRef = ref<InstanceType<typeof ElTable>>();

const filters = reactive<IFilterChangeParams>({});
onMounted(() => {
  if (dataSource.columns) {
    dataSource.columns.forEach((item) => {
      if (item.columnKey) {
        filters[item.columnKey] = [];
      }
    });
  }
});

const computedColumns = computed(() =>
  dataSource.columns.filter((item) => !item.hidden),
);
const handleSizeChange = (pageSize: number) => {
  console.log(pageSize, "pageSize-Table");
  emits("sizeChange", pageSize);
};
const handleCurrentChange = (currentPage: number) => {
  console.log(currentPage, "currentPage-Table");
  emits("currentChange", currentPage);
};

const handleSortChange = (data: ISortChangeParams) => {
  console.log(data, "sort-Table");
  emits("sortChange", data);
};

const handleFilterChange = (filter: IFilterChangeParams) => {
  const newfilters = { ...filters, ...filter };
  filters[Object.keys(filter)[0]] = filter[Object.keys(filter)[0]];
  emits("filterChange", newfilters);
};

defineExpose({
  multipleTableRef,
});
</script>

<style scoped lang="scss">
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>